package controler;

import java.sql.DriverManager;  // gestion des pilotes
import java.sql.Connection;     // une connexion à la BD
import java.sql.Statement;      // une instruction 
import java.sql.SQLException;   // une erreur
import java.util.ArrayList;
import java.util.HashMap;





import model.DocumentXML;
import model.NodeXML;
import model.Term;
import model.TermNode;

public class JDBCHandler {
	
	/**LOADER**/
	public JDBCHandler() {
		super();
		try {
			this.loadDriver();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	// chargement du pilote
	void loadDriver() throws ClassNotFoundException {
	    Class.forName("com.mysql.jdbc.Driver");
	}
	
	// ouverture de connexion
	Connection newConnection() throws SQLException {
		// url bdd
	    final String url = "jdbc:mysql://localhost:3306/index_bdd";	    
	    Connection conn = DriverManager.getConnection(url, "root", "") ;
	    return conn;
	}
	
	
	/** ADDDOCUMENTS **/
	public void addDocuments(ArrayList<DocumentXML> list) throws SQLException{
		
		Connection conn = newConnection();
		StringBuilder requete = new StringBuilder("INSERT INTO Document(idDocument, name, path) VALUES");
		int size = list.size();
		for(int i=0; i<size ; i++){
			DocumentXML documentXML = list.get(i);
			String documentXMLPath = documentXML.getPath().replace("\\", "\\\\");
			//Si c'est le dernier elt on ne met pas la virgule
			if(i == size-1){
				requete.append("("+documentXML.getId()+",'"+documentXML.getName()+"','"+documentXMLPath+"');");
			}else{
				requete.append("("+documentXML.getId()+",'"+documentXML.getName()+"','"+documentXMLPath+"'),");
			}
		}
		
		Statement st = conn.createStatement();
		st.executeUpdate(requete.toString());
		st.close();
		conn.close();
	}
	
	
	/** ADDTERMS **/
	public void addTerms(HashMap<String, Term> hash) throws SQLException{
		
		Connection conn = newConnection();
		StringBuilder requete = new StringBuilder("INSERT INTO Term(idTerm, value) VALUES");
		int cpt = 0;
		int size = hash.size();
		for(Term terme : hash.values()){
			if(cpt == size-1){
				requete.append("("+terme.getIdTerm()+",'"+terme.getValue()+"');");
			}else{
				requete.append("("+terme.getIdTerm()+",'"+terme.getValue()+"'),");
			}
			cpt++;
		}
		Statement st = conn.createStatement();
		st.executeUpdate(requete.toString());
		st.close();
		conn.close();
}
	

	/** ADDNODE **/
	public void addNodes(HashMap<Integer, NodeXML> hash) throws SQLException{
		
		Connection conn = newConnection();
		StringBuilder requete = new StringBuilder("INSERT INTO Node(idNode, path, idDocument) VALUES");
		
		int cpt = 0;
		int size = hash.size();
		for(NodeXML noeud  : hash.values()){
			
			if(cpt == size-1){
				requete.append("("+noeud.getIdNode()+",'"+noeud.getPath()+"',"+noeud.getIdDocument()+");");
			}else{
				requete.append("("+noeud.getIdNode()+",'"+noeud.getPath()+"',"+noeud.getIdDocument()+"),");
			}
			cpt++;
		}
		Statement st = conn.createStatement();
		st.executeUpdate(requete.toString());
		st.close();
		conn.close();
	}


	/** ADDTERMNODES **/
	public void addTermsNodes(HashMap<String, TermNode> hash) throws SQLException{
		
		Connection conn = newConnection();
		StringBuilder requete = new StringBuilder("INSERT INTO TermNode(idTerm, idNode, position, weight, frequency) VALUES");

		int cpt = 0;
		int size = hash.size();
		for(TermNode termNode : hash.values()){
			
			if(cpt == size-1){
				requete.append(" ("+termNode.getIdTerm()+","+termNode.getIdNode()+",'"+termNode.getPosition()+"',"+termNode.getWeight()+","+termNode.getFrequency()+");");
			}else{
				requete.append(" ("+termNode.getIdTerm()+","+termNode.getIdNode()+",'"+termNode.getPosition()+"',"+termNode.getWeight()+","+termNode.getFrequency()+"),");
			}
			cpt++;
		}
		
		Statement st = conn.createStatement();
		st.executeUpdate(requete.toString());
		st.close();
		conn.close();
	}
	
	/** EMPTY DATABASE **/
	public void clearDataBase() throws SQLException{
		Connection conn = newConnection();
		Statement st = conn.createStatement();
		st.executeUpdate("DELETE FROM TermNode;");
		st.executeUpdate("DELETE FROM Term");
		st.executeUpdate("DELETE FROM Node");
		st.executeUpdate("DELETE FROM Document");
		st.close();
		conn.close();
	}
}

